#include<bits/stdc++.h>
using namespace std;
#define fore(i, l, r) for(int i = int(l); i < int(r); i++)
#define sz(a) int((a).size())
int n, m;
vector<int> s, r;
inline bool read() {
if(!(cin >> n >> m))
return false;
s.assign(n, 0);
r.assign(n, 0);
fore(i, 0, n) {
assert(cin >> s[i] >> r[i]);
s[i]--;
}
return true;
}
vector< vector<int> > subs;
inline void solve() {
subs.assign(m + 1, vector<int>());
fore(i, 0, n)
subs[s[i]].push_back(r[i]);
fore(id, 0, sz(subs)) {
sort(subs[id].begin(), subs[id].end());
reverse(subs[id].begin(), subs[id].end());
}
vector<int> mx(n + 5, 0);
fore(id, 0, sz(subs)) {
int curSum = 0;
fore(i, 0, sz(subs[id])) {
curSum += subs[id][i];
if(curSum < 0)
break;
mx[i + 1] += curSum;
}
}
cout << *max_element(mx.begin(), mx.end()) << endl;
}
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
int tt = clock();
#endif
cout << fixed << setprecision(15);
if(read()) {
solve();
#ifdef _DEBUG
cerr << "TIME = " << clock() - tt << endl;
tt = clock();
#endif
}
return 0;
}
1B - Spreadsheet | 1177A - Digits Sequence (Easy Edition) |
1579A - Casimir's String Solitaire | 287B - Pipeline |
510A - Fox And Snake | 1520B - Ordinary Numbers |
1624A - Plus One on the Subset | 350A - TL |
1487A - Arena | 1520D - Same Differences |
376A - Lever | 1305A - Kuroni and the Gifts |
1609A - Divide and Multiply | 149B - Martian Clock |
205A - Little Elephant and Rozdil | 1609B - William the Vigilant |
978B - File Name | 1426B - Symmetric Matrix |
732B - Cormen --- The Best Friend Of a Man | 1369A - FashionabLee |
1474B - Different Divisors | 1632B - Roof Construction |
388A - Fox and Box Accumulation | 451A - Game With Sticks |
768A - Oath of the Night's Watch | 156C - Cipher |
545D - Queue | 459B - Pashmak and Flowers |
1538A - Stone Game | 1454C - Sequence Transformation |